<?php

namespace App\Model;


use Illuminate\Http\Request;

class FinanceDiamondStream extends BaseModel
{
    protected $table = 'finance_diamond_stream';
    protected $primaryKey = 'stream_id';
    public $timestamps = false;
    private $fields = ['from_user_id','diamond_desc','diamond_value','change_time','diamond_type','user_id','diamond_balance','room_id'];
    
    public function getList(Request $request){
        $page = $request->get('page', 0);
        $limit = $request->get('limit', 10);
        $uuid = $request->get('uuid','');
        $nickname = $request->get('nickname','');
        $diamond_type = $request->get('type',null);
        $begin_date = $request->get('begin_date',0);
        $end_date = $request->get('end_date',0);
        $user_id = $request->get('user_id',0);
        
        $query = new self();
        $query = $query->whereHas('number',function ($query) use($uuid, $nickname){
            $query->select(['user_id']);
            if($uuid) $query->where('uuid',$uuid);
            if($nickname) $query->where('nickname','like',"%{$nickname}%");
        });
        
        if($diamond_type >= 0) {
            $query = $query->where('diamond_type',$diamond_type);
        } elseif($diamond_type == -2) {
            $query = $query->whereNotIn('diamond_type', [0,1,2,3]);
        }
        if($user_id) $query = $query->where('user_id',$user_id);
        if($begin_date && $end_date) $query = $query->whereBetween('change_time', [$begin_date, $end_date]);
        if($begin_date && !$end_date) $query = $query->where('change_time','>=',$begin_date);
        if(!$begin_date && $end_date) $query = $query->where('change_time','<=',$end_date);
        

        $diamondStream = $query->orderBy('stream_id','desc')
            // ->with(['number:user_id,uuid,nickname','sender:user_id,uuid,nickname','room:room_id,name'])
            ->paginate($limit,$this->fields,'page',$page);
        $tempDiamondStream = collect($diamondStream)->toArray();
        if(empty($tempDiamondStream['data'])) return $tempDiamondStream;

        $sendUserIdArr = array_column($tempDiamondStream['data'], 'user_id');
        $receiveUserIdArr = array_column($tempDiamondStream['data'],'from_user_id');
        $userIdArr = array_merge($sendUserIdArr, $receiveUserIdArr);

        $users = (new Number())->getNicknameByUserIds($userIdArr);
        $usersMap = array_column($users, null, 'user_id');
        foreach($tempDiamondStream['data'] as &$item) {
            $item['number'] = $usersMap[$item['user_id']] ?? [];
            $item['sender'] = $usersMap[$item['from_user_id']] ?? [];
        }
        unset($item);
        return $tempDiamondStream;
    }
    
    public function number(){
        return $this->belongsTo(Number::class,'user_id','user_id');
    }
    
    /**
     * 接收者
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function sender(){
        return $this->belongsTo(Number::class,'from_user_id','user_id');
    }
    
    /**
     * 房间
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function room(){
        return $this->belongsTo(Room::class,'room_id','room_id');
    }
}
